function HandlePagePaymentBank(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._createElements();
};

HandlePagePaymentBank.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  this._search = DlmanageUtils.acctPaymentSearch();
  
  tr = this._gui.mains.insertRow(-1);
  td = tr.insertCell(-1);
  td.style.backgroundColor = '#fff';
  td.colSpan = 9;
  td.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePagePaymentBank.prototype._loadData = function() {
  this._results = null;
  this._payments = null;
  this._parameters = null;
  this._retrieveResults(1);
  this._retrievePayments(1);
};

HandlePagePaymentBank.prototype._verifyData = function() {
  if (this._results && this._payments) {
    this._updateElements();
  }
};

HandlePagePaymentBank.prototype._retrieveResults = function(page, condition, from, to, pfrom, pto, pos) {
  var _self = this;
  var con = 'd.cancelled = 0' + ' AND d.method =\'' + '{{$smarty.const.PAYMENT_METHOD_BANK|escape:javascript}}' + '\'' + ' AND (d.status = 0)';
  var args = ((condition) ?  'c=' + condition + ' AND ' + con : 'c=' + con) + '&p=' + page + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + ((pfrom) ?  '&pfrom=' + pfrom : '') + ((pto) ?  '&pto=' + pto : '');
  /*new RequestUtils()._custom('acctpayment', args, function(result, params) { _self._results = result;
                                                                             _self._verifyData.call(_self);
                                                                           }, { pos: (pos) ? pos : null });
*/
  new RequestUtils()._mysql('acctpayment', args, function(result, params) { _self._results = result;
                                                                                  _self._verifyData.call(_self);
                                                                                }, {php:'customphp/mysqlcustom.php'});  
};

HandlePagePaymentBank.prototype._retrievePayments = function(page, condition, from, to, pfrom, pto, pos) {
  var _self = this;
  var con = 'd.cancelled = 0' + ' AND d.method =\'' + '{{$smarty.const.PAYMENT_METHOD_BANK|escape:javascript}}' + '\'' + ' AND (d.status = 0)';
  var args = ((condition) ?  'c=' + condition + ' AND ' + con : 'c=' + con) + '&p=' + page  + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + '&s=' + DlmanageUtils.number + ((pfrom) ?  '&pfrom=' + pfrom : '') + ((pto) ?  '&pto=' + pto : '');
  /*new RequestUtils()._custom('acctpayments', args, function(result, params) { _self._payments = result.data;
                                                                              _self._parameters = result;
                                                                              _self._verifyData.call(_self);
                                                                            }, { pos: (pos) ? pos : null });
*/
  new RequestUtils()._mysql('payments', args, function(result, params) { _self._payments = result.data;
                                                                           _self._parameters = result;
                                                                                  _self._verifyData.call(_self);
                                                                                }, {php:'customphp/mysqlcustom.php'});  
};

HandlePagePaymentBank.prototype._updateElements = function() {
  var _self = this, unique = MiscUtils.unique(); 
  DlmanageUtils.clear(this._gui.summarize);
  DlmanageUtils.clear(this._gui.search);
  DOMUtils.removeTableRows(this._gui.mains, 1);
  DOMUtils.removeChildElements(this._gui.pagination);
  DOMUtils.removeChildElements(this._gui.checkAll);
  this._selects = [];
  
  this._gui.summarize.bank.appendChild(document.createTextNode('$' + (this._results.top.receipt).toFixed(2)));
  this._gui.verify.onclick = function() { if (_self._selects.length == 0) {
                                            window.alert('No Items Selected');
                                            return;
                                          }
                                          var payments = [];
                                          for(var i = 0, il = _self._selects.length; i < il; i++) {
                                            var object = _self._selects[i];
                                            object.status = 1;
                                            payments[payments.length] = object;
                                          }
                                          new RequestUtils()._mwrite('payment', payments, [], function() { _self._retrieveResults(1); _self._retrievePayments(_self._parameters.page, _self._parameters.condition);}, { pos: DOMUtils.findPos(this) });
                                        };


  /*--main table--*/
  if (this._payments.length == 0) {
    tr = this._gui.mains.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 9;
    td.style.height = '24px';
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (i = 0, il = this._payments.length; i < il; i++) {
    var payment = this._payments[i];
    if (payment.cancelled == 1) {
      continue;
    }
    tr = this._gui.mains.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'roweven' : 'rowodd';
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    var check = DOMUtils.createInput(null, unique + '_' + i, 'checkbox', '', null);
    check._payment = payment;
    check.onclick = function() { if (this.checked ==  true) {
                                   _self._selects[_self._selects.length] = this._payment;
                                  } else {
                                    MiscUtils.remove(_self._selects, this._payment, 'trackId');
                                  }
                               };
    td.appendChild(check);
    
    td = tr.insertCell(-1);
    td.style.height = '24px';
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 20px';
    td.appendChild(document.createTextNode(SimpleDate.format(payment.createdDate)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 20px';
    td.appendChild(document.createTextNode(SimpleDate.format(payment.date)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 20px';
    td.appendChild(document.createTextNode(DlmanageUtils.customerId(payment.customerId)));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode(payment.stage));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 20px';
    td.appendChild(document.createTextNode(payment.orderFlag));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'left';
    td.style.padding = '0 0 0 20px';
    td.appendChild(document.createTextNode('$' + payment.amount));
    
    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode((payment.method == '{{$smarty.const.PAYMENT_METHOD_BANK|escape:javascript}}') ? 'B' : ''));

    td = tr.insertCell(-1);
    td.style.textAlign = 'center';
    var remove = document.createElement('img');
    remove.src = 'images/common/delete.png';
    remove.style.cursor = 'pointer';
    remove._payment = payment;
    remove.onclick = function() { var pos = DOMUtils.findPos(this);
                                  var div = document.createElement('div');
                                  var payment = this._payment;
                                  var func1 = function() { payment.cancelled = 1;
                                                           payment.cancelledDate = _self._now;
                                                           payment.cancelledOperator = _self._operator.account;
                                                           payment.cancelledMessage = payment._text;
                                                           new RequestUtils()._mwrite('payment', [payment], [], function() { _self._retrieveResults(1); _self._retrievePayments(_self._parameters.page, _self._parameters.condition); }, { pos: pos });
                                                           _self._popupBox2._close();
                                                         };
                                  var func2 = function() { _self._popupBox2._close(); };
                                  MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'Cancel Reason'});
                                  new DialogInput(div, payment, { title: '' });
                                  return false;
                                };
    td.appendChild(remove);
  }
  
  this._gui.checkAll.checked = false;
  this._gui.checkAll._unique = unique;
  this._gui.checkAll.onclick = function() { if (this.checked == true) {
                                             for (var i = 0, il = _self._payments.length; i < il; i++) {
                                                var object = _self._payments[i];
                                                if (document.getElementById(this._unique + '_' + i) && !document.getElementById(this._unique + '_' + i).checked) {
                                                  _self._selects[_self._selects.length] = object;
                                                  document.getElementById(this._unique + '_' + i).checked = true;
                                                }
                                              }
                                            } else {
                                              for (var i = 0, il = _self._payments.length; i < il; i++) {
                                                var object = _self._payments[i];
                                                if (document.getElementById(this._unique + '_' + i)) {
                                                  MiscUtils.remove(_self._selects, object, 'trackId');
                                                  document.getElementById(this._unique + '_' + i).checked = false;
                                                }
                                              }
                                            }
                                          };
  
  Pagination.makePagedResults(this._gui.pagination, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrievePayments.call(_self, page, _self._parameters.condition); }, this, document);
  
  this._paymentSearch(this._gui, function(condition, from, to, pfrom, pto, pos) { _self._retrieveResults.call(_self, 1, condition, from, to, pfrom, pto, pos); _self._retrievePayments.call(_self, 1, condition, from, to, pfrom, pto, pos); });
};

HandlePagePaymentBank.prototype._paymentSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;
  
  gui.search.cdateFrom.value = (this._search.cdateFrom) ? SimpleDate.format(this._search.cdateFrom) : '';
  gui.search.cdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.cdateFrom, function(sd) { _self._search.cdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.cdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.cdateTo.value = (this._search.cdateTo) ? SimpleDate.format(this._search.cdateTo) : '';
  gui.search.cdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.cdateTo, function(sd) { _self._search.cdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  gui.search.cdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
                                             
  gui.search.pdateFrom.value = (this._search.pdateFrom) ? SimpleDate.format(this._search.pdateFrom) : '';
  gui.search.pdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.pdateFrom, function(sd) { _self._search.pdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.pdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.pdateTo.value = (this._search.pdateTo) ? SimpleDate.format(this._search.pdateTo) : '';
  gui.search.pdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.pdateTo, function(sd) { _self._search.pdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  gui.search.pdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                                }
                                              };
  this._search.payer = gui.search.payer.value;
  gui.search.payer.value = this._search.payer;
  gui.search.payer.onkeydown = function() { _self._search.payer = this.value; };
  gui.search.payer.onchange = function() { _self._search.payer = this.value; };
  gui.search.payer.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                              if (code == 13) {
                                                _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                              }
                                            };
  gui.search.payer.onkeyup = function() { if (gui.search.payer.value.length == 0) {
                                              _self._popupBox._close();
                                            } else {
                                              var div = document.createElement('div');
                                              var pos1 = DOMUtils.findPos(gui.search.payer);
                                              var pos = [pos1[0] + 1, pos1[1] + 82];
                                              var query = "SELECT p.customerId FROM ns_Payment as p WHERE 1 = 1 AND " + '((p.customerId LIKE \'%' + gui.search.payer.value + '%\'))' + " ";
                                              MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                              new DialogSearchResult(div, gui.search.payer, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();}, width: 195});
                                            }
                                          };
  gui.search.item.options[gui.search.item.options.length] = new Option('');
  for (var i = 0, il = PaymentStage.array().length; i < il; i++) {
    var stage = PaymentStage.array()[i];
    var option = new Option(stage);
    gui.search.item.options[gui.search.item.options.length] = option;
    if (option.index == this._search.item){
      gui.search.item.selectedIndex = gui.search.item.options.length - 1;
    }
  }
  if (PaymentStage.array().length > 0) {
    this._search.item = (gui.search.item.options[gui.search.item.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_FIRST|escape:'javascript'}}') ? '1' : (gui.search.item.options[gui.search.item.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_SECOND|escape:'javascript'}}') ? '2' : (gui.search.item.options[gui.search.item.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_THIRD|escape:'javascript'}}') ? '3' : (gui.search.item.options[gui.search.item.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_FORTH|escape:'javascript'}}') ? '4' : '';
    gui.search.item.onchange = function() { _self._search.item = (this.options[this.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_FIRST|escape:'javascript'}}') ? '1' : (this.options[this.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_SECOND|escape:'javascript'}}') ? '2' : (this.options[this.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_THIRD|escape:'javascript'}}') ? '3' : (this.options[this.selectedIndex].text == '{{$smarty.const.PAYMENT_STAGE_FORTH|escape:'javascript'}}') ? '4' : ''; };
  }
                                            
  gui.search.sort.options[gui.search.sort.options.length] = new Option('');
  for (var i = 0, il = OrderType.array().length; i < il; i++) {
    var type = OrderType.array()[i];
    var option = new Option(type);
    gui.search.sort.options[gui.search.sort.options.length] = option;
    if (option.text == this._search.sort){
      gui.search.sort.selectedIndex = gui.search.sort.options.length - 1;
    }
  }
  if (OrderType.array().length > 0) {
    this._search.sort = gui.search.sort.options[gui.search.sort.selectedIndex].text;
    gui.search.sort.onchange = function() { _self._search.sort = this.options[this.selectedIndex].text; };
  }
  
  gui.search.amount.value = this._search.amount;
  gui.search.amount.onkeydown = function() { _self._search.amount = this.value; };
  gui.search.amount.onchange = function() { _self._search.amount = this.value; };
  gui.search.amount.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                               }
                                             };
  gui.search.method.options[gui.search.method.options.length] = new Option('');
  for (var i = 0, il = PaymentMethod.array().length; i < il; i++) {
    var method = PaymentMethod.array()[i];
    var option = new Option(method);
    gui.search.method.options[gui.search.method.options.length] = option;
    if (option.text == this._search.method){
      gui.search.method.selectedIndex = gui.search.method.options.length - 1;
    }
  }
  if (PaymentMethod.array().length > 0) {
    this._search.method = gui.search.method.options[gui.search.method.selectedIndex].text;
    gui.search.method.onchange = function() { _self._search.method = this.options[this.selectedIndex].text; };
  }
  
  gui.search.verified.defaultChecked = this._search.verified;
  gui.search.verified.onclick = function() { if(this.checked) {
                                               _self._search.verified = true;
                                             } else {
                                               _self._search.verified = false;
                                             }
                                           };
  gui.search.unverified.defaultChecked = this._search.unverified;
  gui.search.unverified.onclick = function() { if (this.checked) {
                                                 _self._search.unverified = true;
                                               } else {
                                                 _self._search.unverified = false;
                                               }
                                             };
  
  gui.search.search.onclick = function() { _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.cdateFrom) ? JSON.stringify(_self._search.cdateFrom) : null, (_self._search.cdateTo) ? JSON.stringify(_self._search.cdateTo) : null, (_self._search.pdateFrom) ? JSON.stringify(_self._search.pdateFrom) : null, (_self._search.pdateTo) ? JSON.stringify(_self._search.pdateTo) : null, DOMUtils.findPos(this));
                                         };
};

HandlePagePaymentBank.prototype._toString = function(search) {
  var str = '1 = 1';
  
  str += (search.payer == '') ? '' : ((isNaN(search.payer)) ? ' AND 1 = 0' : ' AND (d.customerId=\'' + search.payer + '\')');
  str += (search.amount == '') ? '' : ((isNaN(search.amount)) ? ' AND 1 = 0' : ' AND (d.amount=\'' + search.amount + '\')');
  str += (search.item == '') ? '' : ' AND (d.stage=\'' + search.item + '\')';
  str += (search.sort == '') ? '' : ' AND (d.orderFlag LIKE \'' + search.sort + '%\')';
  str += (search.method == '') ? '' : ' AND (d.method=\'' + search.method + '\')';

  if (search.verified && search.unverified) {
    str += ' AND ((d.status = 1) OR (d.status = 0))';
  } else if (search.verified) {
    str += ' AND (d.status = 1)';
  } else if (search.unverified) {
    str += ' AND (d.status = 0)';
  }
  
  return str;
};

HandlePagePaymentBank.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePagePaymentBank.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePagePaymentBank.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};